package com.dreamchaser.familydevice.util.device;

/***
 *                    _ooOoo_
 *                   o8888888o
 *                   88" . "88
 *                   (| -_- |)
 *                    O\ = /O
 *                ____/`---'\____
 *                 .' \\| |// `.
 *               / \\||| : |||// \
 *             / _||||| -:- |||||- \
 *               | | \\\ - /// | |
 *             | \_| ''\---/'' | |
 *              \ .-\__ `-` ___/-. /
 *           ___`. .' /--.--\ `. . __
 *        ."" '< `.___\_<|>_/___.' >' "".
 *       | | : `- \`.;`\ _ /`;.`/ -` : | |
 *         \ \ `-. \_ __\ /__ _/ .-` / /
 * ======`-.____`-.___\_____/___.-`____.-'======
 *                    `=---='
 *
 * =============================================
 *          佛祖保佑             永无BUG
 *
 *
 * @Author: SJie
 * Date: 2020/8/3 17:41
 * Description:
 *
 */

public class ByteHexUtil {
    byte b = 127;
    private static final char[] HEX_CHARS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    /**
     * byte[]数组转十六进制 空格为分隔符  ff 00 01
     */
    public static String bytesToHexStr(byte[] bytes) {
        int len = bytes.length;
        if (len == 0) {
            return null;
        }
        char[] cbuf = new char[len * 3];
        for (int i = 0; i < len; i++) {
            int x = i * 3;
            cbuf[x] = HEX_CHARS[(bytes[i] >>> 4) & 0xf];
            cbuf[x + 1] = HEX_CHARS[bytes[i] & 0xf];
            cbuf[x + 2] = ' ';
        }
        return new String(cbuf).trim();
    }

    /**
     * byte转十六进制  ff
     */
    public static String bytesToHexStr(byte b) {
        byte[] bytes = {b};
        return bytesToHexStr(bytes).toUpperCase();
    }

    /**
     * byte转数字 ff->255
     *
     * @param b
     * @return
     */
    public static int byteToHex(byte b) {
        byte[] bytes = {b};
        String s = bytesToHexStr(bytes).toUpperCase();
        return Integer.parseInt(s, 16);
    }

    /**
     * 16进制字符串转数字 ff->255
     *
     * @return
     */
    public static int hexStrToHex(String hexStr) {
        return Integer.parseInt(hexStr, 16);
    }

    /**
     * 16进制字符串转数字 ff->255
     *
     * @return
     */
    public static String hexToHexStr(int hex) {
        int[] a = {hex};
        byte[] bytes = hexToBytes(a);
        return bytesToHexStr(bytes[0]);
    }


    /**
     * 十六进制字符串转byte[]数组
     */
    public static byte[] hexStrToBytes(String hexStr, String split) {
        if (split == null || "".equals(split)) {
            if (hexStr.length() % 2 != 0) {//长度为单数
                hexStr = "0" + hexStr;//前面补0
            }
        } else {
            String[] strs = hexStr.split(split);
            hexStr = "";
            for (int i = 0; i < strs.length; i++) {
                if (strs[i].length() == 1) {//长度为单数
                    strs[i] = "0" + strs[i];//前面补0

                }
                hexStr += strs[i];
            }
        }
        char[] chars = hexStr.toCharArray();
        int len = chars.length / 2;
        byte[] bytes = new byte[len];
        for (int i = 0; i < len; i++) {
            int x = i * 2;
            bytes[i] = (byte) Integer.parseInt(String.valueOf(new char[]{chars[x], chars[x + 1]}), 16);
        }
        return bytes;
    }

    /**
     * 十六进制字符串转byte[]数组
     */
    public static byte[] hexStrToBytes(String hexStr) {
        return hexStrToBytes(hexStr, null);
    }

    /**
     * 十六进制数组转byte[]数组
     *
     * @return
     */
    public static byte[] hexToBytes(int[] data) {
        String str = "";
        for (int d : data) {
            String hex = Integer.toHexString(d);
            if (hex.length() == 1) {
                hex = "0" + hex;
            }
            str += hex;
        }
        return hexStrToBytes(str);

    }

    /**
     * byte转二机制字符串
     *
     * @param b
     * @return
     */
    public static String byteToBin(Byte b) {
        return String.format("%08d", Long.parseLong(Long.toString(b & 0xff, 2)));
    }

    /**
     * 十六进制字符串转二机制字符串
     *
     * @param b
     * @return
     */
    public static String hexStrToBin(String b, String split) {
        byte[] bs = hexStrToBytes(b, split);
        StringBuilder s = new StringBuilder();
        for (byte b1 : bs) {
            s.append(byteToBin(b1) + " ");
        }

        return s.toString().trim();
    }

    /**
     * 十六进制字符串转二机制字符串
     *
     * @param b
     * @return
     */
    public static String hexStrToBin(String b) {
        return hexStrToBin(b, null);
    }


    public static void main(String[] args) {
        int[] a = {0xff, 0x01};
        byte[] b = ByteHexUtil.hexToBytes(a);
        System.out.println(b[1]);


    }
}
